package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/admin")
public class DtsAdminAdminController {

    @Autowired
    DtsAdminService adminService;


    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(name = "limit",required = false,defaultValue = "10")Integer limit,
                       String sort, String order,String username){

        IPage<DtsAdmin> iPage =adminService.findByPage(page,limit,sort,order,username);
        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsAdmin admin){
        DtsAdmin dtsAdmin=adminService.add(admin);
        return ResponseUtil.ok(dtsAdmin);
    }

    @RequiresPermissions("admin:admin:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsAdmin admin){
        DtsAdmin dtsAdmin=adminService.update(admin);
        return ResponseUtil.ok(dtsAdmin);
    }


    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsAdmin admin){
        DtsAdmin dtsAdmin=adminService.delete(admin);
        return ResponseUtil.ok(dtsAdmin);
    }
}
